﻿using System;


namespace RoughJs.Ast
{
    /// <summary>
    /// 2010-01-5 ----- 2010-01-29
    /// 陈曦 1.0
    /// Microsoft Public License (Ms-PL)
    ///This license governs use of the accompanying software. If you use the software, you accept this license. If you do not accept the license, do not use the software.
    ///1. Definitions
    ///The terms "reproduce," "reproduction," "derivative works," and "distribution" have the same meaning here as under U.S. copyright law.
    ///A "contribution" is the original software, or any additions or changes to the software.
    ///A "contributor" is any person that distributes its contribution under this license.
    ///"Licensed patents" are a contributor's patent claims that read directly on its contribution.
    ///2. Grant of Rights
    ///(A) Copyright Grant- Subject to the terms of this license, including the license conditions and limitations in section 3, each contributor grants you a non-exclusive, worldwide, royalty-free copyright license to reproduce its contribution, prepare derivative works of its contribution, and distribute its contribution or any derivative works that you create.
    ///(B) Patent Grant- Subject to the terms of this license, including the license conditions and limitations in section 3, each contributor grants you a non-exclusive, worldwide, royalty-free license under its licensed patents to make, have made, use, sell, offer for sale, import, and/or otherwise dispose of its contribution in the software or derivative works of the contribution in the software.
    ///3. Conditions and Limitations
    ///(A) No Trademark License- This license does not grant you rights to use any contributors' name, logo, or trademarks.
    ///(B) If you bring a patent claim against any contributor over patents that you claim are infringed by the software, your patent license from such contributor to the software ends automatically.
    ///(C) If you distribute any portion of the software, you must retain all copyright, patent, trademark, and attribution notices that are present in the software.
    ///(D) If you distribute any portion of the software in source code form, you may do so only under this license by including a complete copy of this license with your distribution. If you distribute any portion of the software in compiled or object code form, you may only do so under a license that complies with this license.
    ///(E) The software is licensed "as-is." You bear the risk of using it. The contributors give no express warranties, guarantees or conditions. You may have additional consumer rights under your local laws which this license cannot change. To the extent permitted under your local laws, the contributors exclude the implied warranties of merchantability, fitness for a particular purpose and non-infringement. 
    /// </summary>
    public class ContextFactory
    {
        public interface Listener
        {
            /**
             * Notify about newly created {@link Context} object.
             */
             void contextCreated(Context cx);

            /**
             * Notify that the specified {@link Context} instance is no longer
             * associated with the current thread.
             */
             void contextReleased(Context cx);
        }

        /// <summary>
        /// 定义特征 但不包括子类
        /// </summary>
        /// <param name="cx">上下文</param>
        /// <param name="featureIndex"></param>
        /// <returns></returns>
        public bool hasFeature(Context cx, int featureIndex)
        {
            int version;
            switch (featureIndex)
            {
                case Context.FEATURE_NON_ECMA_GET_YEAR:
                    /*
                     * During the great date rewrite of 1.3, we tried to track the
                     * evolving ECMA standard, which then had a definition of
                     * getYear which always subtracted 1900.  Which we
                     * implemented, not realizing that it was incompatible with
                     * the old behavior...  now, rather than thrash the behavior
                     * yet again, we've decided to leave it with the - 1900
                     * behavior and point people to the getFullYear method.  But
                     * we try to protect existing scripts that have specified a
                     * version...
                     */
                    version = cx.getLanguageVersion();
                    return (version == Context.VERSION_1_0
                            || version == Context.VERSION_1_1
                            || version == Context.VERSION_1_2);

                case Context.FEATURE_MEMBER_EXPR_AS_FUNCTION_NAME:
                    return false;

                case Context.FEATURE_RESERVED_KEYWORD_AS_IDENTIFIER:
                    return false;

                case Context.FEATURE_TO_STRING_AS_SOURCE:
                    version = cx.getLanguageVersion();
                    return version == Context.VERSION_1_2;

                case Context.FEATURE_PARENT_PROTO_PROPERTIES:
                    return true;

                case Context.FEATURE_E4X:
                    version = cx.getLanguageVersion();
                    return (version == Context.VERSION_DEFAULT
                            || version >= Context.VERSION_1_6);

                case Context.FEATURE_DYNAMIC_SCOPE:
                    return false;

                case Context.FEATURE_STRICT_VARS:
                    return false;

                case Context.FEATURE_STRICT_EVAL:
                    return false;

                case Context.FEATURE_LOCATION_INFORMATION_IN_ERROR:
                    return false;

                case Context.FEATURE_STRICT_MODE:
                    return false;

                case Context.FEATURE_WARNING_AS_ERROR:
                    return false;

                case Context.FEATURE_ENHANCED_JAVA_ACCESS:
                    return false;
            }
            // It is a bug to call the method with unknown featureIndex
            throw new ArgumentException(featureIndex.ToString());
        }

        /// <summary>
        /// 当前线程中创建新的上下文
        /// </summary>
        /// <returns></returns>
        public Context makeContext()
        {
            return new Context(this);
        }

        public void onContextCreated(Context cx)
        {
            //Object listeners = this.listeners;
            //for (int i = 0; ; ++i)
            //{
            //    Listener l = (Listener)Kit.getListener(listeners, i);
            //    if (l == null)
            //        break;
            //    l.contextCreated(cx);
            //}
            //TODO：这里没实现
        }

        public void onContextReleased(Context cx)
        {
            Object listeners = this.listeners;
            for (int i = 0; ; ++i)
            {
                Listener l = (Listener)Kit.getListener(listeners, i);
                if (l == null)
                    break;
                l.contextReleased(cx);
            }
        }

        /**
    * Checks if this is a sealed ContextFactory.
    * @see #seal()
    */
        public bool isSealed()
        {
            return sealed1;
        }
        
        /// <summary>
        /// 得到一个本地的上下文工厂
        /// </summary>
        /// <returns></returns>
        public static ContextFactory getGlobal()
        {
            return global;
        }

        public Object doTopCall(Callable callable,
                               Context cx, Scriptable scope,
                               Scriptable thisObj, Object[] args)
        {
            return callable.call(cx, scope, thisObj, args);
        }

        public Object call(ContextAction action)
        {
            return Context.call(this, action);
        }

        public void observeInstructionCount(Context cx, int instructionCount)
        {
        }


        private static volatile bool hasCustomGlobal;
        private static ContextFactory global = new ContextFactory();

        private volatile bool sealed1;

        private Object listenersLock = new Object();
        private volatile Object listeners;
        private bool disabledListening;
        //  private ClassLoader applicationClassLoader;


    }
}
